package com.net.xpay.core.manager.impl;

import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.net.xpay.core.dto.AccountAmountCountDto;
import com.net.xpay.core.model.*;
import com.net.common.annotation.ReadTransactional;
import com.net.common.annotation.WriteTransactional;
import com.net.common.dao.BasicMapper;
import com.net.common.dao.Page;
import com.net.common.manager.AbstractVersionedManager;
import com.net.noah.core.service.SerialNumberService;
import com.net.xpay.core.dao.PayTransBatchMapper;
import com.net.xpay.core.domain.PayTransBatch;
import com.net.xpay.core.manager.PayTransBatchManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;


@Component
@ReadTransactional
public class PayTransBatchManagerImpl extends AbstractVersionedManager<PayTransBatch> implements PayTransBatchManager {

    @Autowired
    private PayTransBatchMapper mapper;
    @Autowired
    private SerialNumberService serialNumberService;

    @Override
    @WriteTransactional(propagation = Propagation.SUPPORTS)
    public int save(PayTransBatch obj) {
        Preconditions.checkArgument(obj.getId() == null);
        obj.setId(serialNumberService.getLongSerialNumber(getTableFullName()));
        return super.save(obj);
    }

    @Override
    protected BasicMapper<Long, PayTransBatch> getMapper() {
        return mapper;
    }

    @Override
    public PayTransBatch findByImportBatchNo(Long userId, String importBatchNo) {
        Preconditions.checkArgument(userId != null && !Strings.isNullOrEmpty(importBatchNo));
        PayTransBatch condition = new PayTransBatch();
        condition.setUserId(userId);
        condition.setImportBatchNo(importBatchNo);
        return getByCondition(condition);
    }

    @Override
    public PayTransBatch findById(Long userId, Long id) {
        Preconditions.checkArgument(userId != null && id != null);
        PayTransBatch condition = new PayTransBatch();
        condition.setId(id);
        condition.setUserId(userId);
        return getByCondition(condition);
    }

    @Override
    public List<PayTransBatch> search(PayTransBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        Page page = new Page(searchModel);
        page.addOrder("create_time", "desc");
        page.addOrder("id", "desc");

        return nonEmptyList(mapper.search(searchModel, page));
    }

    @Override
    public long searchCount(PayTransBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return mapper.searchCount(searchModel);
    }

    @Override
    public List<PayTransBatch> list(PayTransBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        return nonEmptyList(mapper.list(searchModel));
    }

    /**
     * 统计代码 不要随意改动
     */
    @Override
    public PayTransBatchStatResultModel statLevelOneSuccessPayTransBatch(Long partnerId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(partnerId != null && minEndTime != null && maxEndTime != null);
        return mapper.statLevelOneSuccessPayTransBatch(partnerId, minEndTime, maxEndTime);
    }

    @Override
    public PayTransBatchStatResultModel statLevelTwoSuccessPayTransBatch(Long partnerId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(partnerId != null && minEndTime != null && maxEndTime != null);
        return mapper.statLevelTwoSuccessPayTransBatch(partnerId, minEndTime, maxEndTime);
    }

    @Override
    public UserTransStatModel countAmountAndTimesOfSuccess(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(userId != null && minEndTime != null && maxEndTime != null);
        return mapper.countAmountAndTimesOfSuccess(userId, minEndTime, maxEndTime);
    }

    @Override
    public BigDecimal getTransferLimit(LocalDateTime minEndTime, LocalDateTime maxEndTime, Long userId, Long alipayUserId) {
        Preconditions.checkArgument(minEndTime != null && maxEndTime != null && userId != null && alipayUserId != null);
        return mapper.getTransferLimit(minEndTime, maxEndTime, userId, alipayUserId);
    }


    @Override
    public CompanyBillDailyStatModel statDailyCompanyBill(LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(minEndTime != null && maxEndTime != null);
        return mapper.statDailyCompanyBill(minEndTime, maxEndTime);
    }

    @Override
    public UserPayTransStatCommonModel countNoFreeAmountAndTimesOfSuccess(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(userId != null && minEndTime != null && maxEndTime != null);
        return mapper.countNoFreeAmountAndTimesOfSuccess(userId, minEndTime, maxEndTime);
    }

    @Override
    public PartnerUserPayTransDayStatModel sumPartnerUserPayTransDay(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(userId != null && minEndTime != null && maxEndTime != null);
        return mapper.sumPartnerUserPayTransDay(userId, minEndTime, maxEndTime);
    }

    @Override
    public PartnerUserPayTransDayStatModel sumPartnerPayTransDay(Long partnerId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(partnerId != null && minEndTime != null && maxEndTime != null);
        return mapper.sumPartnerPayTransDay(partnerId, minEndTime, maxEndTime);
    }

    @Override
    public PayTransDailyStatModel statActivePartnerCount(LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        return mapper.statActivePartnerCount(minEndTime, maxEndTime);
    }

    @Override
    public CompanyPayTransDayStatModel sumCompanyPayTransDay(LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        return mapper.sumCompanyPayTransDay(minEndTime, maxEndTime);
    }

    @Override
    public long sumTotalVeryAlipayUserCount(LocalDateTime minEndTime, LocalDateTime maxEndTime) {

        return mapper.sumTotalVeryAlipayUserCount(minEndTime, maxEndTime);
    }

    @Override
    public long sumUserTotalVeryAlipayUserCount(Long userId, LocalDateTime minEndTime, LocalDateTime maxEndTime) {
        Preconditions.checkArgument(userId != null && minEndTime != null && maxEndTime != null);
        return mapper.sumUserTotalVeryAlipayUserCount(userId, minEndTime, maxEndTime);
    }

    @Override
    public List<Long> listIds(PayTransBatchSearchModel searchModel) {
        Preconditions.checkArgument(searchModel != null);
        List<Long> ids = mapper.listIds(searchModel);
        return ids == null ? new ArrayList<>() : ids;
    }

    /**
     * 统计成功打款的金额
     *
     * @param startTime
     * @param endTime
     * @param userId
     * @param dayTime
     * @return
     */
    @Override
    public AccountAmountCountDto sumTransSuccessAmount(LocalDateTime startTime, LocalDateTime endTime, Long userId, LocalDateTime dayTime) {
        Preconditions.checkArgument(userId != null && startTime != null && endTime != null);
        return mapper.sumTransSuccessAmount(userId, startTime, endTime,dayTime);
    }

}
